<?php
/**
 * @title 2420. 找到所有好下标 - 二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer[]
     */
    function goodIndices($nums, $k) {
        /**
         * @since 2024-01-24 好下标集合
         */
        $goodIndexArray = [];

        /**
         * @since 2024-01-24 递减区间，递增区间
         */
        $range1 = $range2 = [];

        /**
         * @since 2024-01-24 遍历数组nums，找递减和递增区间
         */
        $p1 = $p2 = 0;
        $numsCount = count($nums);
        for($i=1; $i<$numsCount; $i++){
            /**
             * @since 2024-01-24 递减
             */
            if($nums[$i] <= $nums[$i-1]){
            } else {
                if($i-1-$p1+1 >= $k){
                    $range1[] = [$p1, $i-1];
                } else {}
                $p1 = $i;
            }

            /**
             * @since 2024-01-24 递增
             */
            if($nums[$i] >= $nums[$i-1]){
            } else {
                if($i-1-$p2+1 >= $k){
                    $range2[] = [$p2, $i-1];
                } else {}
                $p2 = $i;
            }
        }
        if($numsCount-$p1 >= $k){
            $range1[] = [$p1, $numsCount-1];
        } else {}
        if($numsCount-$p2 >= $k){
            $range2[] = [$p2, $numsCount-1];
        } else {}
        // return [$range1, $range2];
        // echo "range1 = ", json_encode($range1), "\n";
        // echo "range2 = ", json_encode($range2), "\n";

        /**
         * @since 2024-01-24 遍历数组nums，找好下标
         */
        for($i=$k; $i<$numsCount-$k; $i++){
            /**
             * @since 2024-01-24 左边区间，右边区间
             */
            $leftRange = [$i-$k, $i-1];
            $rightRange = [$i+1, $i+$k];
            // echo "i={$i}, leftRange=[", $leftRange[0] ,",", $leftRange[1] ,"], rightRange=[", $rightRange[0], ",", $rightRange[1] ,"]\n";

            /**
             * @since 2024-01-24 左区间起点，在range1二分查找
             */
            $s = 0;
            $e = count($range1)-1;
            while($s<=$e){
                $m = ($s+$e)>>1;
                $v = $range1[$m][0];

                if($v <= $leftRange[0]){
                    $s = $m+1;
                } elseif($v > $leftRange[0]){
                    $e = $m-1;
                } else {}
            }

            /**
             * @since 2024-01-24 左区间是否非递增
             */
            // echo "left, s={$s}\n";
            if($s == 0){
                continue;
            } else {
                if($range1[$s-1][1] < $leftRange[1]) {
                    continue;
                } else {}
            }

            /**
             * @since 2024-01-24 右区间起点，在range2二分查找
             */
            $s = 0;
            $e = count($range2)-1;
            while($s<=$e){
                $m = ($s+$e)>>1;
                $v = $range2[$m][0];

                if($v <= $rightRange[0]){
                    $s = $m+1;
                } elseif($v > $rightRange[0]){
                    $e = $m-1;
                } else {}
            }

            /**
             * @since 2024-01-24 右区间是否非递减
             */
            // echo "right, s={$s}\n";
            if($s == 0){
                continue;
            } else {
                if($range2[$s-1][1] < $rightRange[1]) {
                    continue;
                } else {}
            }

            /**
             * @since 2024-01-24 好下标
             */
            $goodIndexArray[] = $i;
        }

        /**
         * @return Integer[]
         */
        return $goodIndexArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.24_9.php
 */
$datas = [
    [[2,1,1,1,3,4,1], 2], // 2,3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->goodIndices(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
